package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.atguigu.common.valid.AddGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author ÁõË¼¸Ç
 * @email liusigai@gmail.com
 * @date 2020-12-08 19:40:47
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:brand:save")
    public R save(@Valid @RequestBody BrandEntity brand,BindingResult bindingResult){
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        Map<String,String> map = new HashMap<>();
        if(bindingResult.hasErrors()){
            fieldErrors.forEach(item->{
                String messae = item.getDefaultMessage(); // 错误提示，在@NotNull(message="xxx不能为空")
                String field = item.getField(); // 是哪个字段有错误
                map.put(field,messae);
            });
            return R.error(400,"提交数据不合法").put("data",map);
        }else{
            brandService.save(brand);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:brand:update")
    public R update(@RequestBody BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

    /**
     * @Validated(value = AddGroup.class)   标示，该校验功能，只在新增时来处罚BrandEntity 里面的字段校验
     * @param brandEntity
     * @param bindingResult
     * @return
     */
    @RequestMapping("add")
    public R add(@Validated(value = AddGroup.class) @RequestBody BrandEntity brandEntity, BindingResult bindingResult){
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        Map<String,String> map = new HashMap<>();
        if(bindingResult.hasErrors()){
            fieldErrors.forEach(item->{
                String messae = item.getDefaultMessage(); // 错误提示，在@NotNull(message="xxx不能为空")
                String field = item.getField(); // 是哪个字段有错误
                map.put(field,messae);
            });
        }else{

        }

        return R.error(400,"提交数据不合法").put("data",map);
    }
}
